// cloudfunctions/updateUserInfo/index.js
const cloud = require('wx-server-sdk');
cloud.init();
const db = cloud.database();

exports.main = async (event, context) => {
  try {
    const { userId, avatarUrl, nickName } = event;
    
    // 参数验证
    if (!userId) {
      return { success: false, message: '用户ID不能为空' };
    }
    
    const updateData = {};
    if (avatarUrl) updateData._avatarUrl = avatarUrl;
    if (nickName) updateData._nickName = nickName;
    
    // 更新用户信息
    await db.collection('users').doc(userId).update({
      data: updateData
    });
    
    return {
      success: true,
      message: '更新成功'
    };
    
  } catch (error) {
    console.error('更新用户信息失败', error);
    return {
      success: false,
      message: '更新失败',
      error: error.message
    };
  }
};